<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/jqEBMessengerIfr.min.js"></script>
</head>
<body>
<div>display none</div>
<script type="text/javascript">

//日志窗口显示控制
var log_debug =$.ebfn.getQueryStringRegExp(document.location.href, 'debug');
if(log_debug && (log_debug=='true' || log_debug=='TRUE')) {
	   $("body").append('<textarea rows="50" cols="100" id="log"></textarea>');
}

function log(text) {
	if($('#log').length>0) {
		var logTextArea =$('#log')[0];
		logTextArea.value =logTextArea.value + text + "\n";
		logTextArea.scrollTop=logTextArea.scrollHeight;
	}
}

log("version=2014-06-05");

//log("send cookie:" + document.cookie);
var fr_name= $.ebfn.getQueryStringRegExp(document.location.href, 'fr_name');
if(fr_name=="")
	fr_name ="iframe1";

var ifrMessenger = new IfrMessenger(fr_name);
ifrMessenger.listen(function (jsonString) {
	log("listen cookie:" + document.cookie);
	log(jsonString);
	var jsonMsg =$.evalJSON(jsonString);
	if(jsonMsg.pv)
		log($.Base64.decode(jsonMsg.pv));
	var data =null;
	if(jsonMsg.json2kv) {
		data =jsonMsg.pv?$.evalJSON($.Base64.decode(jsonMsg.pv)):null;
	} else {
		data =jsonMsg.pv? $.Base64.decode(jsonMsg.pv):null;
	}

    $.ajax({
        url: jsonMsg.url,
        type: jsonMsg.submitType,
        data: data,
        dataType: "text",
        contentType:"application/x-www-form-urlencoded",
        processData: (jsonMsg.json2kv==null)?true:jsonMsg.json2kv,
        async: true,
        cache:false,
        timeout: jsonMsg.timeout,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var jsonData ={
            		xhr: XMLHttpRequest,
            		error: errorThrown,
            		status: textStatus,
                	tag: jsonMsg.tag
                };
            sendMessage($.toJSON(jsonData));
            
        },
        success: function (data, textStatus) {
            log("data:\n"+data+"status:"+textStatus);
            var jsonData ={
                    status: textStatus,
                    tag: jsonMsg.tag,
                    data: data? $.Base64.encode(data):null
                };
            sendMessage($.toJSON(jsonData));
        }
    });
});

ifrMessenger.addTarget(window.parent, 'parent');

function sendMessage(jsonMsg) {
	log("send cookie:" + document.cookie);
    ifrMessenger.targets['parent'].send(jsonMsg);
}
</script>
</body>
</html>